package com.taptech.doufu.ui.dialog;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import com.taptech.doufu.R;
import com.taptech.doufu.event.BaseEventBusBean;
import com.taptech.doufu.event.EventBusConstant;
import com.taptech.doufu.net.retrofit.api.RxJavaHelper;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Action1;

public class AppGlobalDialog extends Activity {

    private View layLoading;
    private ObjectAnimator animator;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void dismiss(BaseEventBusBean event){
        if (event != null && EventBusConstant.TAG_DISMISS_GLOBAL_DIALOG.equals(event.getTag())) {
            finish();
        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_app_global);
        if (Build.VERSION.SDK_INT != Build.VERSION_CODES.O) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        layLoading = findViewById(R.id.layLoading);
        layLoading.setVisibility(View.VISIBLE);
        ImageView ivLoading = findViewById(R.id.ivLoading);
        EventBus.getDefault().register(this);
        animator = ObjectAnimator.ofFloat(ivLoading, "rotation", 0f, 360f);
        animator.setRepeatCount(Animation.INFINITE);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(1000);
        animator.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (animator != null) {
            animator = null;
        }
        EventBus.getDefault().unregister(this);
    }

    public static void showDialog(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, AppGlobalDialog.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    public static void dismissDialog() {
        BaseEventBusBean eventBus = new BaseEventBusBean();
        eventBus.setTag(EventBusConstant.TAG_DISMISS_GLOBAL_DIALOG);
        EventBus.getDefault().post(eventBus);
    }

    @Override
    public void finish() {
        layLoading.setVisibility(View.GONE);
        Observable.timer(1000, TimeUnit.MILLISECONDS)
                .compose(RxJavaHelper.<Long>observeOnMainThread())
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        AppGlobalDialog.super.finish();
                    }
                });
    }
}
